package com.szjz.elasticsearch.swagger;

import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 访问链接<br>
 * http://localhost:xxxx/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Autowired
    private SwaggerProperties swaggerProperties;

    private static final List<Parameter> parameters = new ArrayList<>();

    private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerConfiguration.class);

    private static final String ADMIN = "admin";
    private static final String PORTAL = "portal";
    private static final String AUTH = "auth";
    private static final String BASE = "base";


    /**
     * 可以存在多个docket 一个bean 代表一组
     * @ConditionalOnProperty 满足配置属性为true的条件才能生成bean
     */


    /**
     * 控制台服务
     */
    @Bean
    @ConditionalOnProperty(value = "swagger.groupName.admin", matchIfMissing = false)
    public Docket adminDocket() {
        LOGGER.info("【Swagger】开启控制台服务文档");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)//
                .apiInfo(apiInfo())//
                .select()//
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.ant())//
                .paths(PathSelectors.any())
//                .pathMapping("/")//
                .build();//
//                .groupName(ADMIN); //在分布式开发中整合swagger的时候不能添加组名，因为每个微服务就是一个组

        builder(docket);

        return docket;
    }


    /**
     * 移动端服务
     */
    @Bean
    @ConditionalOnProperty(value = "swagger.groupName.portal", matchIfMissing = false)
    public Docket portalDocket() {
        LOGGER.info("【Swagger】开启移动端服务文档");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)//
                .select()//
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/*/portal/**"))//
                .paths(PathSelectors.any())
                .build()//
//                .groupName(PORTAL)
                .pathMapping("/")//
                .globalOperationParameters(parameters)//
                .apiInfo(apiInfo());//

        builder(docket);

        return docket;
    }


    /**
     * 登录服务
     */
    @Bean
    @ConditionalOnProperty(value = "swagger.groupName.auth", matchIfMissing = false)
    public Docket authApi() {
        LOGGER.info("【Swagger】开启登录服务文档");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)//
                .select()//
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/*/login/**"))//
                .build()//
//                .groupName(AUTH)
                .pathMapping("/")//
                .globalOperationParameters(parameters)//
                .apiInfo(apiInfo());//

        builder(docket);

        return docket;
    }

    /**
     * 基础服务
     */
    @Bean
    @ConditionalOnProperty(value = "swagger.groupName.base", matchIfMissing = false)
    public Docket baseApi() {
        LOGGER.info("【Swagger】开启基础服务文档");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)//
                .select()//
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/*/base/**"))//
                .build()//
//                .groupName(BASE)
                .pathMapping("/")//
                .globalOperationParameters(parameters)//
                .apiInfo(apiInfo());//

        builder(docket);

        return docket;
    }


    private void builder(Docket docket) {
        ApiSelectorBuilder builder = docket.select();
        if (swaggerProperties.getScanPackage() != null) {
            LOGGER.info("【Swagger】 scanPackage: {}", swaggerProperties.getScanPackage());
            builder.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getScanPackage()));
        }
//        if (!StringUtils.isEmpty(swaggerProperties.getPath())) {
//            LOGGER.info("【Swagger】 path: {}", swaggerProperties.getPath());
//            builder.paths(PathSelectors.ant(swaggerProperties.getPath()));
//        }
    }


    private ApiInfo apiInfo() {
        LOGGER.info("【swagger】 title: {}", swaggerProperties.getTitle());
        LOGGER.info("【swagger】 desc: {}", swaggerProperties.getDesc());
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())                                      //标题
                .description(swaggerProperties.getDesc())                               //描述
                .contact(new Contact("石中君子", "", ""))
                .version("1.0")
                .build();

    }

}
